'Copyright (c) 2008, Jonathan Allen
Imports ClrExtensions
Imports WrapNetflix
Imports ClrExtensions.Net.Rest


''' <summary>
''' This class represents a generic Netflix resource
''' </summary>
''' <remarks></remarks>
Public MustInherit Class NetflixResource
    Implements INetflixResource
    Private m_Source As XElement
    Private ReadOnly m_Connection As NetflixConnection

    Friend ReadOnly Property Connection() As NetflixConnection
        Get
            Return m_Connection
        End Get
    End Property

    Public MustOverride ReadOnly Property Id() As String Implements INetflixResource.Id
    Public MustOverride ReadOnly Property Title() As String Implements INetflixResource.Title

    Public Property Source() As XElement
        Get
            Return m_Source
        End Get
        Protected Set(ByVal value As XElement)
            m_Source = value
        End Set
    End Property

    Protected Sub New(ByVal connection As NetflixConnection, ByVal source As XElement)
        m_Connection = connection
        m_Source = source
    End Sub

    Public Function FindCategories(ByVal categoryScheme As String) As IList(Of String)
        Dim result = (From node In Source.<category> Where node.@scheme = categoryScheme Select node.@label).ToList
        Return result
    End Function

    Public Function FindLink(ByVal linkTitle As String) As ResourceLink
        Return FindLink(Connection, Source, linkTitle)
    End Function

    Public Function FindRelLink(ByVal linkRel As String) As ResourceLink
        Return FindRelLink(Connection, Source, linkRel)
    End Function

    Public Shared Function FindLink(ByVal connection As NetflixConnection, ByVal root As XElement, ByVal linkTitle As String) As ResourceLink
        Dim temp = (From node In root.<link> Where node.@title = linkTitle Select node).FirstOrDefault
        If temp IsNot Nothing Then Return New ResourceLink(connection, temp) Else Return Nothing
    End Function

    Public Shared Function FindRelLink(ByVal connection As NetflixConnection, ByVal root As XElement, ByVal linkRel As String) As ResourceLink
        Dim temp = (From node In root.<link> Where node.@rel = linkRel Select node).FirstOrDefault
        If temp IsNot Nothing Then Return New ResourceLink(connection, temp) Else Return Nothing
    End Function

End Class
